package com.tai.network;

import org.junit.Test;

import java.io.*;

/**
 * The type Stream reader writer.
 *
 * @author taylor
 * @Description: 转换流 ：处理字节流和字符流的转换
 * @date 2021 /6/22 21:49
 */
public class StreamReaderWriter {

    /**
     * DK1.7之后try-with-resource处理机制 自动关闭资源
     */
    @Test
    public void test() {
        try (FileInputStream fis = new FileInputStream("temp/念奴娇·赤壁怀古.txt");
             FileOutputStream fos = new FileOutputStream("temp/赤壁怀古.txt");
             InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
             OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
             BufferedReader br = new BufferedReader(isr);
             BufferedWriter bw = new BufferedWriter(osw);)
        {
//            BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream("temp/念奴娇·赤壁怀古.txt"), "UTF-8"));
//            BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("temp/赤壁怀古.txt"),"GBK"));
            String data;
            while ((data = br.readLine()) != null) {
                bw.write(data);
                bw.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
